class TestLiquidfun {

    public particleColor:b2ParticleColor[];
    public world:b2World;
    private timeStep:number = 1.0 / 60.0;
    private velocityIterations:number = 8;
    private positionIterations:number = 3;
    public test:TestBase;
    public g_groundBody:b2Body;

    constructor(){

        this.particleColor = [
            new  b2ParticleColor(0xff,0x00,0x00,0xff),   // red
            new b2ParticleColor(0x00, 0xff, 0x00, 0xff), // green
            new b2ParticleColor(0x00, 0x00, 0xff, 0xff), // blue
            new b2ParticleColor(0xff, 0x8c, 0x00, 0xff), // orange
            new b2ParticleColor(0x00, 0xce, 0xd1, 0xff), // turquoise
            new b2ParticleColor(0xff, 0x00, 0xff, 0xff), // magenta
            new b2ParticleColor(0xff, 0xd7, 0x00, 0xff), // gold
            new b2ParticleColor(0x00, 0xff, 0xff, 0xff) // cyan
        ]
    }

    public initTestbed():void
    {
        let gravity:b2Vec2 = new b2Vec2(0,-10);
        this.world = new b2World(gravity);
        this.testBed();
    }

    public testBed():void
    {
        this.testSwitch("test1");
        this.render();
    }

    public testSwitch(testname){
        this.resetWorld();
        this.world.SetGravity(new b2Vec2(0,-10));
        var bd = new b2BodyDef();
        this.g_groundBody = this.world.CreateBody(bd);
        this.test = new TestDamBreak(this.world);
        //this.testDamBreak();
    }
    public render():void
    {
        var that = this;
        that.Step();
        requestAnimationFrame(this.render.bind(this));
    }

    public resetWorld():void
    {
        if(this.world != null)
        {
            while(this.world.joints.length > 0)
            {
                this.world.DestroyJoint(this.world.joints[0]);
            }
            while(this.world.bodies.length > 0)
            {
                this.world.DestroyBody(this.world.bodies[0]);
            }
            while(this.world.particleSystems.length > 0)
            {
                this.world.DestroyParticleSystem(this.world.particleSystems[0]);
            }
        }
    }

    public Step():void
    {
        this.world.Step(this.timeStep, this.velocityIterations, this.positionIterations)
    }

    public onMousedown(event:any){
        var p:b2Vec2 = new b2Vec2();
        var aabb:b2AABB = new b2AABB();
        var queryCallback = new QueryCallback(p);
        this.world.QueryAABB(queryCallback, aabb);
    }

    public testDamBreak(){
        var bodyDef:b2BodyDef = new b2BodyDef();
        var ground:b2Body = this.world.CreateBody(bodyDef);

        
        var chainShape:b2ChainShape = new b2ChainShape();
        chainShape.vertices.push(new b2Vec2(-2,0));
        chainShape.vertices.push(new b2Vec2(2,0));
        chainShape.vertices.push(new b2Vec2(2,4));
        chainShape.vertices.push(new b2Vec2(-2,4));

        chainShape.CreateLoop();
        
        var s:b2Shape = new b2CircleShape();
        //ground.CreateFixtureFromShape(s,1);
        var fd:b2FixtureDef = new b2FixtureDef();
        fd.shape = s;
        fd.density =0;
        ground.CreateFixtureFromDef(fd);

        var shape:b2PolygonShape = new b2PolygonShape();
        shape.SetAsBoxXYCenterAngle(0.8,1,new b2Vec2(-1.2,1.01), 0);

        var psd = new b2ParticleSystemDef;
        psd.radius = 0.025;
        psd.dampingStrength = 0.2;

        var particleSystem = this.world.CreateParticleSystem(psd);

        var pd = new b2ParticleGroupDef();
        pd.shape = shape;
        var group = particleSystem.CreateParticleGroup(pd);   
    }

}
class QueryCallback{

    public point:b2Vec2;
    public fixture:b2Fixture;

    constructor(point:b2Vec2){
        this.point = point;
        this.fixture = null;
    }

    public ReportFixture(fixture:b2Fixture):boolean
    {
        var body:b2Body = fixture.body;
        if(body.GetType() == b2_dynamicBody)
        {
            var inside:boolean = fixture.TestPoint(this.point);
            if(inside){
                this.fixture = fixture;
                return true;
            }
        }
        return false;
    }
}